package com.itheima.controller;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.po.UploadFile;
import com.itheima.service.FilenameService;
/**
 * 文件上传
 */
@Controller
public class FileUploadController {

		@Autowired
		private FilenameService filenameService;

	@RequestMapping("/fileUpload")
		public String handleFormUpload(@RequestParam("username") String name,
			@RequestParam("uploadfile") List<MultipartFile> uploadfile,
			HttpServletRequest request,Model model) {
		
			return   filenameService.handleFormUpload(name, uploadfile, request, model);
		
		}
	
	 @RequestMapping(value = {"/","/users"})
	    public String findByPage(@RequestParam(required = false) Integer pageNum , @RequestParam(required = false) Integer pageSize, Model model){
	        if(pageNum==null){
	            pageNum=1;
	        }
	        if(pageSize==null){
	            pageSize=8;
	        }
	        filenameService.findByPage(pageNum,pageSize);
	        PageInfo<UploadFile> pageInfo =filenameService.findByPage(pageNum,pageSize);

	        //封装数据传递到view层
	        model.addAttribute("pageInfo",pageInfo);
	       //跳转解析页面 /fileList.jsp
	        return "fileList";
	    }
	 
	    @RequestMapping("/findByCondition")
	    public String findByCondition(String filename,Model model){
	        PageHelper.startPage(1,8);
	         List<UploadFile> users =filenameService.findByCondition(filename);
	        PageInfo<UploadFile> pageInfo =new PageInfo<>(users);
	        model.addAttribute("pageInfo",pageInfo);
//	        跳转到解析页面  /list.jsp
	        return "fileList";
	    }
	    
	    @RequestMapping("/edit")
	    public String addFile(Integer id ,Model model){
	    	  if(id!=null){ //修改
	    		  UploadFile  uploadFile= filenameService.findById(id);
	              model.addAttribute("uploadFile",uploadFile);
	              System.out.println(model.toString());
	              return "forward:/update.jsp";
	          }
	    	  System.out.println("id..."+id);
	    	return  "forward:/fileUpload.jsp";
	    }
	    
	    @PostMapping("/addFile" )
	    public String addFile( UploadFile uploadFile,@RequestParam("uploadfile")  List<MultipartFile> filename,HttpServletRequest request,Model model){
	        
	    	String newFileName =filenameService.handleFormUpload(uploadFile.getUsername(), filename, request, model);
	    	
	    	uploadFile.setFilename(newFileName);
	        int i =filenameService.addFile(uploadFile);
	        System.out.println(uploadFile.toString());
	        System.out.println("i。。。。。"+i);
	        return "redirect:/users";
	    }
	    
	    //只是修改了数据中的文件名，实际路径的文件名更改没有实现
	    @RequestMapping("/updateFile")
	    public String updateUploadFile(HttpServletRequest request ,UploadFile uploadfile ,Model model) throws UnsupportedEncodingException{

	    System.out.println("........."+filenameService.getFilename(request, uploadfile.getFilename()));
	    	System.out.println("......"+uploadfile.toString());
	    	
	    	String oldFileName = request.getParameter("oldFilename");
	    	//System.out.println("旧文件名。。。。"+request.getParameter("oldFilename"));
	        int i =filenameService.updateUploadFile(uploadfile);
	        filenameService.FixFileName(request, oldFileName, uploadfile.getFilename());
	        return "redirect:/users";
	    }
	    
	    @RequestMapping("/del")
	    public String del(Integer id){
	        int i =filenameService.deleteFile(id);
	        //删除成功之后查询所有用户信息
	        return "redirect:/users";
	    }
	
}
